package com.github.livebase.plugins;

/**
 * @ClassName RegisterInfo
 * @description:
 * @author: biao532
 * @create: 2024-01-21 09:27
 * @Version 1.0
 **/
public class RegisterInfo {
    private Class clazz;
    private Object object;
    private String key;
    private String[] busEvent;
    private boolean onlyListener;

    private PluginContext context;
    private Object lock = new Object();
    public RegisterInfo(Class clazz) {
        this(clazz, null, null, false);
    }
    public RegisterInfo(Class clazz, String[] busEvent, boolean onlyListener) {
        this(clazz, null, busEvent, onlyListener);
    }
    public RegisterInfo(Class clazz, String key, String[] busEvent) {
        this(clazz, key, busEvent, false);
    }
    public RegisterInfo(Class clazz, String key, String[] busEvent, boolean onlyListener) {
        this.clazz = clazz;
        this.busEvent = busEvent;
        this.key = key;
        this.onlyListener = onlyListener;
    }

    public void setContext(PluginContext context) {
        this.context = context;
    }

    public PluginContext getContext() {
        return context;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public Class getClazz() {
        return clazz;
    }

    public String[] getBusEvent() {
        return busEvent;
    }

    public void setBusEvent(String[] busEvent) {
        this.busEvent = busEvent;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public void setOnlyListener(boolean onlyListener) {
        this.onlyListener = onlyListener;
    }

    public boolean isOnlyListener() {
        return onlyListener;
    }

    public Object getObject() throws IllegalAccessException, InstantiationException {
        if (object == null) {
            synchronized (lock) {
                if (object == null) {
                    object = clazz.newInstance();
                }
            }
        }
        return object;
    }
}
